/*
 * Dalek.cpp
 *
 *  Created on: 23/05/2013
 *      Author: roger
 */

#include "Dalek.h"


Dalek::Dalek() {
}
void Dalek::init() {
#ifdef DEBUG_DALEK
	Serial.print(getName());Serial.println(INIT);
#endif
	m_icon = new Icon(28,&dalek_bitmap[0]);
	m_height = Graphics.height();
	m_width = Graphics.width();
	m_cy = Graphics.height() / 2;
	m_cx = Graphics.width() / 2;
	currentColour = BLUE;
}
void Dalek::setup() {
#ifdef DEBUG_DALEK
	Serial.println(PSTR("Dalek setup"));
#endif
	Graphics.fillScreen(BLACK);
	Graphics.setRotation(0);
	Graphics.setCursor(0,0);
	m_height = Graphics.height();
	m_width = Graphics.width();
	m_cy = Graphics.height() / 2;
	m_cx = Graphics.width() / 2;
	srand (0);
	for (unsigned int i=0;i<DALEK_COUNT;i++) {
		daleks[i] = NULL;
	}
	lastx = Hardware.getAccelerometerValues().x;
}
void Dalek::display() {
	Graphics.drawCircle(m_cx,m_cy,RADIUS*circleSize,BLACK);
	if (++circleSize > 8) {
		circleSize = 1;
		int x = Hardware.getAccelerometerValues().x;
		int deltaX = abs(x-lastx);
		lastx = x;
		// if the x axis has moved then attempt to generate a dalek in one of the free slots
		if (deltaX > SENSITIVITY) {
			for (unsigned int i=0;i<DALEK_COUNT;i++) {
				if (daleks[i] == NULL) {
					daleks[i] = new DalekInstance(10+(rand() % (m_width-20)),10+(rand() % (m_height-20)),rand() % 5000000L, &m_smallIcon);
					break;
				}
			}
		}
	}
	for (unsigned int i=0;i<DALEK_COUNT;i++) {
		DalekInstance *d = daleks[i];
		if (d != NULL) {
			if (d->getVanishTime() < micros()) {
				d->erase();
				delete d;
				daleks[i] = NULL;
			}
		}
	}
	if (circleSize == 1) {
		int dalekCounter=0;
		for (unsigned int i=0;i<DALEK_COUNT;i++) {
			if (daleks[i] != NULL) {
				dalekCounter++;
			}
		}
		if (dalekCounter > 0) {
			Hardware.siren();
		}
#ifdef DEBUG_DALEK
		Serial.print(PSTR("dalekCounter "));
		Serial.println(dalekCounter);
#endif
	}
	Graphics.drawCircle(m_cx,m_cy,RADIUS*circleSize,currentColour);
}

Dalek::~Dalek() {
}

App *dalek = new Dalek();
